<template>
	<view>
	
		<block v-if="total">
			<view class="collection_btn">
				<text @click="editBtn">{{editText}}</text>
			</view>
			<!-- 	<checkbox-group @change="checkboxGroupChange"> -->

			<view class="collection_box">
				<view class="collection_list" v-for="(sitem,sindex) in list" :key="sindex">
					<text class="iconfont" :class="sitem.checked?'icondanxuanfuxuan':'iconico2'"
						:style="{'color':sitem.checked?'#FF7133':'#999',fontSize:'40rpx'}" @click="selectCheck(sindex)"
						v-if="edit" />
					<!-- <checkbox :value="sitem.id" :checked="sitem.checked" v-if="edit" color="#FF7133" /> -->
					<view class="flex" @click="pageInfo(sitem)">
						<image :src="imgRemote+sitem.goods.main_image" class="image" width="200" height="200"
							mode="widthFix" border-radius="10">
							<!-- <view slot="error" style="font-size: 24rpx;">加载失败</view> -->
						</image>
						<view class="collection_right">
							<view class="collection_name">{{sitem.goods.name}}</view>
							<view class="collection_price">￥{{sitem.goods.sku_json[0].market_price}}</view>
						</view>
					</view>

				</view>
			</view>


			<!-- </checkbox-group> -->
			<view class="collection_del" v-if="edit">
				<text class="iconfont" :class="allchecked?'icondanxuanfuxuan':'iconico2'"
					:style="{'color':allchecked?'#FF7133':'#999',fontSize:'40rpx'}" @click="allChange"
					 />
			<!-- 	<checkbox-group @change="allChange">
					<checkbox :checked="allchecked" color="#FF7133" />

				</checkbox-group> -->
				<view class="del" @click="del">删除</view>
			</view>

		</block>
		<block v-else>
			<view class="no_list">
				<image src="../../static/img/no_good.png" mode="widthFix"></image>
				<view class="gray_font fs-13">
					暂时没有收藏的商品哦~
				</view>
			</view>

		</block>
	</view>
</template>

<script>
	let {
		imgRemote
	} = getApp().globalData;
	export default {
		data() {
			return {
				background: {
					background: '#fff'
				},
				imgRemote: imgRemote,
				allchecked: false,
				editText: '编辑',
				edit: false,
				ids: [],
				list: [],
				page: 1,
				total: 0
			}
		},
		methods: {
			editBtn() {
				this.edit = !this.edit
				this.editText = this.edit ? '取消编辑' : '编辑'
			},
			selectCheck(index) {
				this.list[index].checked  = !this.list[index].checked ;
				this.allchecked = this.list.every((item) => {
					return item.checked == true;
				})
			},
			allChange(e) {
				this.allchecked = !this.allchecked;
				
				if (this.allchecked) {

					this.list.forEach(item => {
						item.checked = true
					})
				} else {
					this.list.forEach(item => {
						item.checked = false
					})
				}
			},
			getCollection() {
				let params = {
					page: this.page
				}
				this.$get(this.$api.cLists, params, (res) => {
					let {
						data
					} = res;
					if (data.code == 1) {
						data.data.data.map((item) => {
							item.checked = false;
							return item;
						})
						console.log(data.data)
						this.list = this.list.concat(data.data.data);
						this.total = data.data.total;
					}
				})
			},
			del() {
				
				this.$showModal('确定取消选中商品收藏吗？', () => {
					let id = '';
					
					for (let i of this.list) {
						if (i.checked == true) {
							id += i.goods.id + ',';
						}
					}
					let ids = id.substr(0, id.length - 1);
					let params = {
					
						id: ids
					}

					this.$get(this.$api.cDel, params, (res) => {
						let {
							data
						} = res;
						if (data.code == 1) {
							this.page = 1;
							this.list=[];
							this.getCollection();
							this.$Toast('取消成功');
							
						} else {
							this.$Toast(data.msg);
						}
					})
				})
			},
			pageInfo(item) {
				this.$u.route({
					url: '/pages/index/shopdetail?id=' + item.goods_id
				})
			}
		},
		onLoad() {
			this.page = 1;
			this.getCollection();
		},
		onShow() {
		
		},
		onReachBottom() {
			this.page++;

		}
	}
</script>

<style lang="scss">
	.collection_btn {
		height: 80rpx;
		line-height: 80rpx;
		background-color: #fff;
		text-align: right;
		font-size: 22rpx;
		color: #E82600;
		padding: 0 30rpx;
		margin-bottom: 16rpx;
	}

	.collection_box {
		padding: 0 30rpx;
		background: #fff;

		.collection_list {
			display: flex;
			align-items: center;
			padding: 30rpx 0;
			border-bottom: 2rpx solid #eee;

			image {
				width: 200rpx;
				height: 200rpx;
				border-radius: 10rpx;
			}

			.collection_right {
				margin-left: 30rpx;
				position: relative;
				height: 200rpx;
				flex: 1;
				margin-left: 30rpx;

				.collection_name {
					font-size: 30rpx;
					color: #333333;
					display: -webkit-box;
					-webkit-box-orient: vertical;
					-webkit-line-clamp: 3;
					overflow: hidden;
				}

				.collection_price {
					font-size: 30rpx;
					color: #CC0000;
					position: absolute;
					left: 0;
					bottom: 0;
				}
			}
		}
	}

	.collection_del {
		position: fixed;
		bottom: 0;
		left: 0;
		width: 100%;
		height: 98rpx;
		background: #fff;
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding: 0 30rpx;
		box-sizing: border-box;

		.del {
			width: 160rpx;
			height: 60rpx;
			background: $m-c;
			border-radius: 30rpx;
			color: #fff;
			text-align: center;
			line-height: 60rpx;
			font-size: 30rpx;
		}

	}

	.no_list {
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;

		image {
			margin: 364rpx 0 40rpx;
			width: 60%;
		}

	}
</style>
